// pages/user/index/index.js
const api = require("../../apis/modules/user")
var QRCode = require("../../utils/weapp-qrcode")
const app = getApp();
var qrcode;
const W = wx.getSystemInfoSync().windowWidth;
const rate = 750.0 / W;
// 300rpx 在6s上为 150px
const code_w = 300 / rate;
let nowId 
let locked
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dialogShow: false,
    code_w: code_w,
    healthStatus: "健康状态",
    statusList: ["健康状态", "疑似状态", "确诊状态"],
    statusColor: '',
    statusColorList: ["green", 'orange', 'red']
  },
  onLoad: function (options) {
    let  role = wx.getStorageSync('userInfor').role
    if(role){
      this.isVaildRole()
    }
  },
  isVaildRole() {
    let parma = {
      role: wx.getStorageSync('userInfor').role
    }
    api.isVaild(parma).then(res => {
      if (res.resultCode == 0) {
        if (res.data.roleCode == 0) {
          nowId =  0
        } else if (res.data.roleCode == 1) {
          let info = wx.getStorageSync('userInfor')
          info.role = 1
          wx.setStorageSync('userInfor', info)
          nowId = 1
        } else if (res.data.roleCode == 2) {
          nowId =  2
        } else if (res.data.roleCode == 3) {
          let info = wx.getStorageSync('userInfor')
          info.role = 0
          wx.setStorageSync('userInfor', info)
          nowId =  3
        }
        locked = res.data.locked
      }
    })
  },
  toStaffPage() {
    //工作人员登录
    if (this.data.lock) return;
    let token = wx.getStorageSync('staffToken')
    if (token) {
      wx.redirectTo({
        url: '/pages/staff/staffHome/staffHome',
      })
    } else {
      wx.navigateTo({
        url: '/pages/staff/staffLogin/staffLogin'
      })
    }

  },
  toUserCenter() {
    let token = wx.getStorageSync('userToken')
    if (token) {
      this.isVaildRole()
       if(!locked){
        if (nowId == 0 || nowId == 1) {
            wx.navigateTo({
              url: '/pages/user/index/index',
            })
          } else  if (nowId == 2){
            wx.showToast({
              title: '您尚未认证成功，请联系相关人员认证！',
              icon: "none"
            })
        } else {
          wx.showToast({
            title: '您目前非住户身份，请联系相关人员！',
            icon: "none"
          })
        }
      }else {
        wx.showToast({
          title: '您目前非住户身份，请联系相关人员！',
          icon: "none"
        })
      }
    } else {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }

  },
  toBaseInform() {
    let token = wx.getStorageSync('userToken')
    if (token) {
      this.isVaildRole()
      api.getHealthInfo().then(res => {
        if (res.resultCode == 0) {
          if (nowId == 0 || nowId == 1) {
            wx.showToast({
              title: '您已填写登记表，可前往个人中心查看!',
              icon: 'none'
            })
          } else {
            wx.navigateTo({
              url: '/pages/baseInform/inform?showDetail=true',
            })
          }
        } else {
          wx.navigateTo({
            url: '/pages/baseInform/inform',
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  toScanPage() {
    let token = wx.getStorageSync('userToken')
    if (token) {
      this.isVaildRole()
      if(!locked){
      if (nowId == 0 || nowId == 1) {
            let id = wx.getStorageSync('userInfor').id + ""
            api.getHealthScan().then(res => {
              if (res.resultCode == 0) {
                if (res.status == 4) {
                  wx.showToast({
                    title: '您尚未认证成功，请联系相关人员认证！',
                    icon: "none"
                  })
                } else {
                  this.setData({
                    dialogShow: true,
                    healthStatus: this.data.statusList[res.data.status],
                    statusColor: this.data.statusColorList[res.data.status]
                  })
                }
              }
            })
            qrcode = new QRCode('canvas', {
              text: id,
              image: '/images/bg2.jpg',
              width: code_w,
              height: code_w,
              colorDark: "#000",
              colorLight: "white",
              correctLevel: QRCode.CorrectLevel.H,
            });
          }  else   if (nowId == 2){
            wx.showToast({
              title: '您尚未认证成功，请联系相关人员认证！',
              icon: "none"
            })
        } else {
          wx.showToast({
            title: '您目前非住户身份，请联系相关人员！',
            icon: "none"
          })
        }
      }else {
        wx.showToast({
          title: '您目前非住户身份，请联系相关人员！',
          icon: "none"
        })
      }
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  customerservice() {
    wx.makePhoneCall({
      phoneNumber: "15625510645" //仅为示例，并非真实的电话号码
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})